# Maintainer: Konstantin Gizdov <arch at kge dot pw>
# Maintainer: Torsten Keßler <tpkessler at archlinux dot org>

pkgname=intel-oneapi-openmp
pkgver=2023.2.0
_debpkgrel=49495
_prev_pkgver=2023.1.0
_prev_debpkgrel=46305
pkgrel=1
pkgdesc="Intel oneAPI OpenMP runtime library"
arch=('loong64' 'x86_64')
url='https://software.intel.com/content/www/us/en/develop/tools/oneapi.html'
license=("custom")
source=("https://apt.repos.intel.com/oneapi/pool/main/${pkgname}-${pkgver}-${_debpkgrel}_amd64.deb"
        "https://apt.repos.intel.com/oneapi/pool/main/${pkgname}-${pkgver}-${pkgver}-${_debpkgrel}_amd64.deb"
        "https://apt.repos.intel.com/oneapi/pool/main/${pkgname}-common-${pkgver}-${pkgver}-${_debpkgrel}_all.deb"
        "https://apt.repos.intel.com/oneapi/pool/main/intel-oneapi-runtime-openmp-${pkgver}-${_debpkgrel}_amd64.deb"
        "https://apt.repos.intel.com/oneapi/pool/main/intel-oneapi-runtime-openmp-common-${_prev_pkgver}-${_prev_debpkgrel}_all.deb")
b2sums=('c8718187c25a6b9b15ba059854a7c2fb37bc46754d52da92690b550e4a5f73fc9eb9eb941ca8aad5edba60a6dbb55d045760755e144ba867f62bd32981b70950'
        '23168aafc6fcea235d030a718f9c86609290319a867b7e41f15f3b7fc453a0e504aab43ab15aba9a24af33d8bd5232361d9d7eb4e3b9741abdc8cfb91de67714'
        '2caf5a902fbed887cf3031b47122e36f12f881740fdc5ca27d96d34f66d801f87d38bc68a381ae2fc3961096bc4e40b462c4849f1b7501844b514a511ab7e879'
        'dd11253ec0c807e937096e401cce9a4ce57f92ab89d557354bb5401c8e7a980f31e8028460e8887fc7df54d7f964d9b20f754862fd00e0ff8eea425aa928518d'
        'f18519ac7c93fd01a69bd42a193497c4a49d460e259e7819b0ac02256d680f63409084a8af79c9516e6f83d21e93be763d2ef84969a26e0c04b0404458de32d8')
depends=('intel-oneapi-common' 'libelf' 'level-zero-loader' 'libffi')
noextract=(
    "${pkgname}-${pkgver}-${_debpkgrel}_amd64.deb"
    "${pkgname}-${pkgver}-${pkgver}-${_debpkgrel}_amd64.deb"
    "${pkgname}-common-${pkgver}-${pkgver}-${_debpkgrel}_all.deb"
    "intel-oneapi-runtime-openmp-${pkgver}-${_debpkgrel}_amd64.deb"
    "intel-oneapi-runtime-openmp-common-${_prev_pkgver}-${_prev_debpkgrel}_all.deb"
)
conflicts=('intel-oneapi-basekit')

package() {
    cd "${srcdir}"

    ar x "${srcdir}/${pkgname}-${pkgver}-${_debpkgrel}_amd64.deb"
    tar xvf data.tar.xz -C "${pkgdir}"
    rm data.tar.xz

    ar x "${srcdir}/${pkgname}-${pkgver}-${pkgver}-${_debpkgrel}_amd64.deb"
    tar xvf data.tar.xz -C "${pkgdir}"
    rm data.tar.xz

    ar x "${srcdir}/${pkgname}-common-${pkgver}-${pkgver}-${_debpkgrel}_all.deb"
    tar xvf data.tar.xz -C "${pkgdir}"
    rm data.tar.xz

    ar x "${srcdir}/intel-oneapi-runtime-openmp-${pkgver}-${_debpkgrel}_amd64.deb"
    tar xvf data.tar.xz -C "${pkgdir}"
    rm data.tar.xz

    ar x "${srcdir}/intel-oneapi-runtime-openmp-common-${_prev_pkgver}-${_prev_debpkgrel}_all.deb"
    tar xvf data.tar.xz -C "${pkgdir}"
    rm data.tar.xz

    # drop conda binaries
    rm -r "${pkgdir}"/opt/intel/oneapi/conda_channel

    # fix permissions
    find "${pkgdir}" -type f \( -iname '*.so' -o -iname '*.so.*' \) -exec chmod +x '{}' \;


    # fix pkgconfig
    local _prefix=/opt/intel/oneapi/compiler
    sed -e "s@prefix=.*@prefix=${_prefix}/latest@g" \
        -i "${pkgdir}/${_prefix}/${pkgver}"/lib/pkgconfig/openmp.pc
    install -d "${pkgdir}"/usr/share/pkgconfig
    ln -s {"${_prefix}"/latest/lib/pkgconfig/,"${pkgdir}"/usr/share/pkgconfig/}openmp.pc

    # ldconfig
    install -d "${pkgdir}"/etc/ld.so.conf.d
    echo "${_prefix}"/${pkgver}/"$(sed -n 's/libdir=${prefix}\///p' "${pkgdir}/${_prefix}/${pkgver}"/lib/pkgconfig/openmp.pc)" \
        > "${pkgdir}/etc/ld.so.conf.d/${pkgname}.conf"
    echo "/opt/intel/oneapi/lib/intel64" >> "${pkgdir}/etc/ld.so.conf.d/${pkgname}.conf"
    echo "${_prefix}/latest/linux/compiler/lib/intel64" >> "${pkgdir}/etc/ld.so.conf.d/${pkgname}.conf"

    install -d "${pkgdir}"/usr/share/licenses/"${pkgname}"
    ln -s /usr/share/licenses/intel-oneapi "${pkgdir}"/usr/share/licenses/"${pkgname}"/oneapi
    ln -s /opt/intel/oneapi/compiler/"${pkgver}"/licensing/openmp "${pkgdir}"/usr/share/licenses/"${pkgname}"/openmp
}
